﻿Imports System.Text
Imports System.Collections.ObjectModel

<DebuggerDisplay("{ToString()}")>
Public Class DFAState

    Private m_outEdges As List(Of DFAEdge)
    Public ReadOnly Property OutEdges() As ReadOnlyCollection(Of DFAEdge)
        Get
            Return m_outEdges.AsReadOnly()
        End Get
    End Property

    Sub New()
        m_outEdges = New List(Of DFAEdge)
        m_nfaStateSet = New SortedSet(Of Integer)
    End Sub

    Private m_nfaStateSet As SortedSet(Of Integer)
    Public ReadOnly Property NFAStateSet As ISet(Of Integer)
        Get
            Return m_nfaStateSet
        End Get
    End Property

    Friend Sub AddEdge(ByVal edge As DFAEdge)
        CodeContract.RequiresArgumentNotNull(edge, "edge")

        m_outEdges.Add(edge)
    End Sub

    Friend Sub RemoveEdge(ByVal edge As DFAEdge)
        CodeContract.RequiresArgumentNotNull(edge, "edge")

        m_outEdges.Remove(edge)
    End Sub

    Public ReadOnly Property IsAcceptState() As Boolean
        Get
            Return TokenTypeIndex <> 0
        End Get
    End Property

    Friend Property TokenTypeIndex As Integer

    Private m_index As Integer
    Public Property Index() As Integer
        Get
            Return m_index
        End Get
        Friend Set(ByVal value As Integer)
            m_index = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Dim stateStrBuilder As New StringBuilder

        With stateStrBuilder
            .Append("State #")
            .Append(Index)
            If IsAcceptState Then
                .Append(" Accept #")
                .Append(TokenTypeIndex)
            End If

            .Append(" {")

            Dim i As Integer = 0
            For Each stateIndex In m_nfaStateSet
                If i > 0 Then .Append(", ")
                .Append(stateIndex)

                i += 1
            Next

            .Append("}"c)

            Return stateStrBuilder.ToString
        End With

    End Function
End Class
